﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Net;
using System.Net.Mail;
using System.Collections.Generic;
using System.Xml;
using Microsoft.SqlServer.Server;

/// <summary>
/// Summary description for ReadSBData
/// </summary>
/// 

namespace GetNivdalData
{
    
    
    public class GetNivdalData
    {

        public static string ReplaceLineBreaks(object Input)
        {
            return (Input.ToString().Replace("\r\n", "<br /> ").Replace("\n", "<br /> "));

        }

        public static string TotalNewbieTips = "6";

        public static string FormattedNumber(object Num)
        {
            //GetNivdalData.GetNivdalData.FormattedNumber()
            string ReturnValue = Num.ToString();
            int Value;

            if (int.TryParse(ReturnValue, out Value))
            {
                ReturnValue = Value.ToString("N0");
            }

            return ReturnValue;            
        }

        public static string GetIconByMatchType(object o)
        {
            string ReturnValue = "~/Images/Icons/";
            //string Type = (string)o;
            int Type;
            try
            {
                Type = Int32.Parse(o.ToString());
            }
            catch
            {
                Type = 0;
            }


            switch (Type)
            {
                case 1:
                    ReturnValue += "LeagueIcon.png";
                    break;
                case 2:
                    ReturnValue += "NationalCupIcon.png";
                    break;
                case 3:
                    ReturnValue += "InternationalCupIcon.png";
                    break;
                case 6:
                    ReturnValue += "FriendlyCupRulesIcon.png";
                    break;
                default:
                    ReturnValue += "FriendlyIcon.png";
                    break;
            }
            return (ReturnValue);
        }

        public static string GetIconByMatchType(object o, bool IsBonusPromotionsMatch)
        {
            string ReturnValue = "~/Images/Icons/";
            //string Type = (string)o;
            int Type;
            try
            {
                Type = Int32.Parse(o.ToString());
            }
            catch
            {
                Type = 0;
            }

            if (IsBonusPromotionsMatch)
                ReturnValue += "BonusPromotionsIcon.png";
            else switch (Type)
            {
                case 1:
                    ReturnValue += "LeagueIcon.png";
                    break;
                case 2:
                    ReturnValue += "NationalCupIcon.png";
                    break;
                case 3:
                    ReturnValue += "InternationalCupIcon.png";
                    break;
                case 6:
                    ReturnValue += "FriendlyCupRulesIcon.png";
                    break;
                default:
                    ReturnValue += "FriendlyIcon.png";
                    break;
            }
            return (ReturnValue);
        }

        public static string GetToolTipByMatchType(object o)
        {
            string ReturnValue = "";
            string Type = o.ToString();

            switch (Type)
            {
                case "1":
                    ReturnValue += Resources.Resource.Global_League;
                    break;
                case "2":
                    ReturnValue += Resources.Resource.Global_NationalCup;
                    break;
                case "3":
                    ReturnValue += Resources.Resource.Global_InternationalCup;
                    break;
                case "4":
                    ReturnValue += Resources.Resource.Global_Friendly;
                    break;
                case "6":
                    ReturnValue += Resources.Resource.FriendDetails_FriendlyWithCupRules;
                    break;
                default:
                    ReturnValue += Resources.Resource.Global_Friendly;
                    break;
            }
            return (ReturnValue);
        }

        public static string GetToolTipByMatchType(object o, bool IsBonusPromotionsMatch)
        {
            string ReturnValue = "";
            string Type = o.ToString();
            if (IsBonusPromotionsMatch)
                ReturnValue += Resources.Resource.BonusPromotions_PageTitle;
            else switch (Type)
            {
                case "1":
                    ReturnValue += Resources.Resource.Global_League;
                    break;
                case "2":
                    ReturnValue += Resources.Resource.Global_NationalCup;
                    break;
                case "3":
                    ReturnValue += Resources.Resource.Global_InternationalCup;
                    break;
                case "4":
                    ReturnValue += Resources.Resource.Global_Friendly;
                    break;
                case "6":
                    ReturnValue += Resources.Resource.FriendDetails_FriendlyWithCupRules;
                    break;
                default:
                    ReturnValue += Resources.Resource.Global_Friendly;
                    break;
            }
            return (ReturnValue);
        }

        public static decimal GetIpCountryV2(string IPAddress)
        {
            decimal CountryId = 0;

            // read the IP address
            Nivdal.IPToCountry.WSIP2CountrySoapClient client = new Nivdal.IPToCountry.WSIP2CountrySoapClient();
            string countryCode = client.GetCountryCodeAuth(IPAddress, "pastch", "pasha2@2");

            if (countryCode.Length == 2)
            {
                // if the country code is 2 digit length - find the country for it
                GetNivdalData GND = new GetNivdalData();
                try
                {
                    CountryId = decimal.Parse(GND.ReadNivdalIntData("CountryCode", "Nivdal_GetCountryIdByCountryCode", countryCode));
                }
                catch
                {
                    CountryId = 0;
                }
            }
            return CountryId;
        }

        public static string GetIPCountrystring(string IPAddress)
        {
            GetNivdalData GND = new GetNivdalData();
            string CountryName = "unknown";
            try
            {
                CountryName = GND.ReadNivdalStringData("IP", "Nivdal_GetCountryStringByDecimalIP", IP2Decimal(IPAddress));
            }
            catch { }
            return CountryName;
        }
        public static decimal GetIPCountry(string IPAddress)
        {
            GetNivdalData GND = new GetNivdalData();
            decimal CountryId = 0;
            
            try
            {
                CountryId = decimal.Parse(GND.ReadNivdalIntData("IP", "Nivdal_GetCountryIdBydecimalIP", IP2Decimal(IPAddress)));
            }
            catch { }
            return CountryId;
        }

        private static long IP2Decimal(string DottedIP)
        {
            string[] arrConvert;
            int i;
            long intResult = 0;
            try
            {
                arrConvert = DottedIP.Split('.');
                for (i = arrConvert.Length - 1; i >= 0; i--)
                {
                    intResult = intResult + ((long.Parse(arrConvert[i]) % 256) * long.Parse(Math.Pow(256, 3 - i).ToString()));
                }
                return intResult;
            }
            catch
            {
                return 0;
            }
        }

        private static DataTable GetLocation(string ipaddress)
        {
            //Create a WebRequest
            WebRequest rssReq = WebRequest.Create("http://freegeoip.appspot.com/xml/" + ipaddress);

            //Create a Proxy
            WebProxy px = new WebProxy("http://freegeoip.appspot.com/xml/" + ipaddress, true);

            //Assign the proxy to the WebRequest
            rssReq.Proxy = px;
            
            //Set the timeout in Seconds for the WebRequest
            rssReq.Timeout = 2000;
            try
            {
                //Get the WebResponse 
                WebResponse rep = rssReq.GetResponse();
                
                //Read the Response in a XMLTextReader
                XmlTextReader xtr = new XmlTextReader(rep.GetResponseStream());

                //Create a new DataSet
                DataSet ds = new DataSet();
                
                //Read the Response into the DataSet
                ds.ReadXml(xtr);
                return ds.Tables[0];
            }

            catch
            { return null; }

        }



        public static string GetTipNumber(object strN)
        {
            // the process of adding new tip:
            // - add 2 tips to Nivdal_NiewbieTipsGetTip SP
            // - fix the flow in this function
            // - fix TotalNewbieTips constant
            // - fix the flow in master page:
            // - Page load
            // - MoveToPreviousTip
            // - MoveToNextTip
            // -    

            string ReturnValue = strN.ToString();
            if (ReturnValue == "2")
                ReturnValue = "1";
            else if ((ReturnValue == "3") || (ReturnValue == "4"))
                ReturnValue = "2";
            else if ((ReturnValue == "5") || (ReturnValue == "6"))
                ReturnValue = "3";
            else if ((ReturnValue == "7") || (ReturnValue == "8"))
                ReturnValue = "4";
            else if ((ReturnValue == "9") || (ReturnValue == "10"))
                ReturnValue = "5";
            else if ((ReturnValue == "11") || (ReturnValue == "12"))
                ReturnValue = "6";
            else if ((ReturnValue == "13") || (ReturnValue == "14"))
                ReturnValue = "7";
            else if ((ReturnValue == "15") || (ReturnValue == "16"))
                ReturnValue = "8";
            else if (ReturnValue == "90" || ReturnValue == "99" || ReturnValue == "100")
                ReturnValue = TotalNewbieTips;

            return ReturnValue;

        }

        public String ReturnUserId()
        {
            MembershipUser mu = Membership.GetUser();
            Guid ProviderUserKey;
            String UserId;
            try
            {
                ProviderUserKey = (Guid)mu.ProviderUserKey;
                UserId = ProviderUserKey.ToString();
            }
            catch
            {
                UserId = null;
            }
            return UserId;
        }

        ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

        public string ReadNivdalFloatData(string ParameterName, string StoredProcedureName, Object Value)
        {
            string returnValue = "error";
            string ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
            using (SqlConnection myConnection = new SqlConnection(ConnectionString))
            {
                SqlCommand storedProcCommand = new SqlCommand(StoredProcedureName, myConnection);
                storedProcCommand.CommandType = CommandType.StoredProcedure;
                storedProcCommand.Parameters.AddWithValue(ParameterName, Value);
                
                SqlDataReader myReader;
                try
                {
                    myConnection.Open(); 
                    myReader = storedProcCommand.ExecuteReader();
                    while (myReader.Read())
                        returnValue = (myReader.GetFloat(0).ToString());
                    // always call Close when done reading.
                    myReader.Close();

                }
                catch (Exception SQLDBException)
                {
                    //Page.ClientScript.RegisterStartupScript(this.GetType(), "Error", "<script type=\"text/javascript\">alert('" + SQLDBException.Message + "');</script>");
                    return Convert.ToString(SQLDBException); //error
                }

                finally
                {
                    // Close the connection when done with it.
                    myConnection.Close();

                }

                return returnValue;

            }
        }



        ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

        public Boolean ReadNivdalBoolData(string ParameterName, string StoredProcedureName, Object Value)
        {
            Boolean returnValue = false;
            string ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
            using (SqlConnection myConnection = new SqlConnection(ConnectionString))
            {
                SqlCommand storedProcCommand = new SqlCommand(StoredProcedureName, myConnection);
                storedProcCommand.CommandType = CommandType.StoredProcedure;
                storedProcCommand.Parameters.AddWithValue(ParameterName, Value);
                
                SqlDataReader myReader;
                try
                {
                    myConnection.Open();
                    myReader = storedProcCommand.ExecuteReader();
                    while (myReader.Read())
                        returnValue = (myReader.GetBoolean(0));
                    // always call Close when done reading.
                    myReader.Close();

                }
                catch
                {
                    //Page.ClientScript.RegisterStartupScript(this.GetType(), "Error", "<script type=\"text/javascript\">alert('" + SQLDBException.Message + "');</script>");
                    return false;
                }

                finally
                {
                    // Close the connection when done with it.
                    myConnection.Close();

                }

                return returnValue;

            }
        }
        ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

        public Boolean IsUserPremium()
        {
            Boolean returnValue = false;
            String UI = ReturnUserId();
            string ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
            using (SqlConnection myConnection = new SqlConnection(ConnectionString))
            {
                SqlCommand storedProcCommand = new SqlCommand("Nivdal_IsUserPremium", myConnection);
                storedProcCommand.CommandType = CommandType.StoredProcedure;
                storedProcCommand.Parameters.AddWithValue("userId", UI);
                
                SqlDataReader myReader;
                try
                {
                    myConnection.Open(); 
                    myReader = storedProcCommand.ExecuteReader();
                    while (myReader.Read())
                        returnValue = (myReader.GetBoolean(0));
                    // always call Close when done reading.
                    myReader.Close();

                }
                catch
                {
                    //Page.ClientScript.RegisterStartupScript(this.GetType(), "Error", "<script type=\"text/javascript\">alert('" + SQLDBException.Message + "');</script>");
                    return false;
                }

                finally
                {
                    // Close the connection when done with it.
                    myConnection.Close();

                }
            }
            return returnValue;
            

        }

        ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

        public Boolean ReadNivdalBoolData(string Parameter1Name, string Parameter2Name, string StoredProcedureName, Object Value1, Object Value2)
        {
            Boolean returnValue = false;
            using (SqlConnection myConnection = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
            {
                SqlCommand storedProcCommand = new SqlCommand(StoredProcedureName, myConnection);
                storedProcCommand.CommandType = CommandType.StoredProcedure;
                storedProcCommand.Parameters.AddWithValue(Parameter1Name, Value1);
                storedProcCommand.Parameters.AddWithValue(Parameter2Name, Value2);
                
                SqlDataReader myReader;
                try
                {
                    myConnection.Open(); 
                    myReader = storedProcCommand.ExecuteReader();
                    while (myReader.Read())
                        returnValue = (myReader.GetBoolean(0));
                    // always call Close when done reading.
                    myReader.Close();

                }
                catch
                {
                    //Page.ClientScript.RegisterStartupScript(this.GetType(), "Error", "<script type=\"text/javascript\">alert('" + SQLDBException.Message + "');</script>");
                    return false;
                }

                finally
                {
                    // Close the connection when done with it.
                    myConnection.Close();

                }
            }
            return returnValue;


        }

        ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

        public string ReadNivdalIntData(string Parameter1Name, string Parameter2Name, string Parameter3Name, string StoredProcedureName, Object Value1, Object Value2, Object Value3)
        {
            //Int16 returnValue = 0;
            string returnValue = "";
            SqlConnection myConnection = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
            SqlCommand storedProcCommand = new SqlCommand(StoredProcedureName, myConnection);
            storedProcCommand.CommandType = CommandType.StoredProcedure;
            storedProcCommand.Parameters.AddWithValue(Parameter1Name, Value1);
            storedProcCommand.Parameters.AddWithValue(Parameter2Name, Value2);
            storedProcCommand.Parameters.AddWithValue(Parameter3Name, Value3);
            
            SqlDataReader myReader;
            try
            {
                myConnection.Open(); 
                myReader = storedProcCommand.ExecuteReader();
                while (myReader.Read())
                    returnValue = (myReader.GetSqlDecimal(0).ToString());
                // always call Close when done reading.
                myReader.Close();

            }
            //catch (Exception SQLDBException)
            //{

            //    //Page.ClientScript.RegisterStartupScript(this.GetType(), "Error", "<script type=\"text/javascript\">alert('" + SQLDBException.Message + "');</script>");
            //    //return Convert.ToString(SQLDBException);
            //    return 

            //    //return 4;
            //}
            catch { }

            finally
            {
                // Close the connection when done with it.
                myConnection.Close();

            }

            return returnValue;


        }

        ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

        public string ReadNivdalIntData(string Parameter1Name, string Parameter2Name, string Parameter3Name, string Parameter4Name, string StoredProcedureName, Object Value1, Object Value2, Object Value3, Object Value4)
        {
            //Int16 returnValue = 0;
            string returnValue = "";
            SqlConnection myConnection = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
            SqlCommand storedProcCommand = new SqlCommand(StoredProcedureName, myConnection);
            storedProcCommand.CommandType = CommandType.StoredProcedure;
            storedProcCommand.Parameters.AddWithValue(Parameter1Name, Value1);
            storedProcCommand.Parameters.AddWithValue(Parameter2Name, Value2);
            storedProcCommand.Parameters.AddWithValue(Parameter3Name, Value3);
            storedProcCommand.Parameters.AddWithValue(Parameter4Name, Value4);
            
            SqlDataReader myReader;
            try
            {
                myConnection.Open(); 
                myReader = storedProcCommand.ExecuteReader();
                while (myReader.Read())
                    returnValue = (myReader.GetSqlDecimal(0).ToString());
                // always call Close when done reading.
                myReader.Close();

            }
            //catch (Exception SQLDBException)
            //{

            //    //Page.ClientScript.RegisterStartupScript(this.GetType(), "Error", "<script type=\"text/javascript\">alert('" + SQLDBException.Message + "');</script>");
            //    return Convert.ToString(SQLDBException);
            //    //return 4;
            //}
            catch { }

            finally
            {
                // Close the connection when done with it.
                myConnection.Close();

            }

            return returnValue;


        }

        ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


        public string ReadNivdalIntData(string Parameter1Name, string Parameter2Name, string Parameter3Name, string Parameter4Name, string Parameter5Name, string StoredProcedureName, Object Value1, Object Value2, Object Value3, Object Value4, Object Value5)
        {
            //Int16 returnValue = 0;
            string returnValue = "";
            SqlConnection myConnection = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
            SqlCommand storedProcCommand = new SqlCommand(StoredProcedureName, myConnection);
            storedProcCommand.CommandType = CommandType.StoredProcedure;
            storedProcCommand.Parameters.AddWithValue(Parameter1Name, Value1);
            storedProcCommand.Parameters.AddWithValue(Parameter2Name, Value2);
            storedProcCommand.Parameters.AddWithValue(Parameter3Name, Value3);
            storedProcCommand.Parameters.AddWithValue(Parameter4Name, Value4);
            storedProcCommand.Parameters.AddWithValue(Parameter5Name, Value5);

            
            SqlDataReader myReader;
            try
            {
                myConnection.Open(); 
                myReader = storedProcCommand.ExecuteReader();
                while (myReader.Read())
                    returnValue = (myReader.GetSqlDecimal(0).ToString());
                // always call Close when done reading.
                myReader.Close();

            }
            //catch (Exception SQLDBException)
            //{

            //    //Page.ClientScript.RegisterStartupScript(this.GetType(), "Error", "<script type=\"text/javascript\">alert('" + SQLDBException.Message + "');</script>");
            //    return Convert.ToString(SQLDBException);
            //    //return 4;
            //}
            catch { }

            finally
            {
                // Close the connection when done with it.
                myConnection.Close();

            }

            return returnValue;


        }


        /////////////////////////////////////////////////////////////////////////////////////////


        public string ReadNivdalIntData(string Parameter1Name, string Parameter2Name, string Parameter3Name, string Parameter4Name, string Parameter5Name, string Parameter6Name, string StoredProcedureName, Object Value1, Object Value2, Object Value3, Object Value4, Object Value5, Object Value6)
        {
            //Int16 returnValue = 0;
            string returnValue = "";
            SqlConnection myConnection = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
            SqlCommand storedProcCommand = new SqlCommand(StoredProcedureName, myConnection);
            storedProcCommand.CommandType = CommandType.StoredProcedure;
            storedProcCommand.Parameters.AddWithValue(Parameter1Name, Value1);
            storedProcCommand.Parameters.AddWithValue(Parameter2Name, Value2);
            storedProcCommand.Parameters.AddWithValue(Parameter3Name, Value3);
            storedProcCommand.Parameters.AddWithValue(Parameter4Name, Value4);
            storedProcCommand.Parameters.AddWithValue(Parameter5Name, Value5);
            storedProcCommand.Parameters.AddWithValue(Parameter6Name, Value6);

            
            SqlDataReader myReader;
            try
            {
                myConnection.Open(); 
                myReader = storedProcCommand.ExecuteReader();
                while (myReader.Read())
                    returnValue = (myReader.GetSqlDecimal(0).ToString());
                // always call Close when done reading.
                myReader.Close();

            }
            //catch (Exception SQLDBException)
            //{

            //    //Page.ClientScript.RegisterStartupScript(this.GetType(), "Error", "<script type=\"text/javascript\">alert('" + SQLDBException.Message + "');</script>");
            //    return Convert.ToString(SQLDBException);
            //    //return 4;
            //}
            catch { }

            finally
            {
                // Close the connection when done with it.
                myConnection.Close();

            }

            return returnValue;


        }
        /////////////////////////////////////////////////////////////////////////////////////////


        public string ReadNivdalIntData(string Parameter1Name, string Parameter2Name, string Parameter3Name, string Parameter4Name, string Parameter5Name, string Parameter6Name, string Parameter7Name, string StoredProcedureName, Object Value1, Object Value2, Object Value3, Object Value4, Object Value5, Object Value6, Object Value7)
        {
            //Int16 returnValue = 0;
            string returnValue = "";
            SqlConnection myConnection = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
            SqlCommand storedProcCommand = new SqlCommand(StoredProcedureName, myConnection);
            storedProcCommand.CommandType = CommandType.StoredProcedure;
            storedProcCommand.Parameters.AddWithValue(Parameter1Name, Value1);
            storedProcCommand.Parameters.AddWithValue(Parameter2Name, Value2);
            storedProcCommand.Parameters.AddWithValue(Parameter3Name, Value3);
            storedProcCommand.Parameters.AddWithValue(Parameter4Name, Value4);
            storedProcCommand.Parameters.AddWithValue(Parameter5Name, Value5);
            storedProcCommand.Parameters.AddWithValue(Parameter6Name, Value6);
            storedProcCommand.Parameters.AddWithValue(Parameter7Name, Value7);

            
            SqlDataReader myReader;
            try
            {
                myConnection.Open(); 
                myReader = storedProcCommand.ExecuteReader();
                while (myReader.Read())
                    returnValue = (myReader.GetSqlDecimal(0).ToString());
                // always call Close when done reading.
                myReader.Close();

            }
            //catch (Exception SQLDBException)
            //{

            //    //Page.ClientScript.RegisterStartupScript(this.GetType(), "Error", "<script type=\"text/javascript\">alert('" + SQLDBException.Message + "');</script>");
            //    return Convert.ToString(SQLDBException);
            //    //return 4;
            //}
            catch { }

            finally
            {
                // Close the connection when done with it.
                myConnection.Close();

            }

            return returnValue;


        }
        /////////////////////////////////////////////////////////////////////////////////////////

        public Boolean ReadNivdalBoolData(string StoredProcedureName)
        {
            Boolean returnValue = false;
            using (SqlConnection myConnection = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
            {
                SqlCommand storedProcCommand = new SqlCommand(StoredProcedureName, myConnection);
                storedProcCommand.CommandType = CommandType.StoredProcedure;
                
                SqlDataReader myReader;
                try
                {
                    myConnection.Open();
                    myReader = storedProcCommand.ExecuteReader();
                    while (myReader.Read())
                        returnValue = (myReader.GetBoolean(0));
                    // always call Close when done reading.
                    myReader.Close();

                }
                //catch
                //{
                //    //Page.ClientScript.RegisterStartupScript(this.GetType(), "Error", "<script type=\"text/javascript\">alert('" + SQLDBException.Message + "');</script>");
                //    return false;
                //}
                catch { }

                finally
                {
                    // Close the connection when done with it.
                    myConnection.Close();

                }
            }
            return returnValue;


        }

        ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

        public Boolean ReadNivdalBoolData(string Parameter1Name, string Parameter2Name, string Parameter3Name, string StoredProcedureName, Object Value1, Object Value2, Object Value3)
        {
            Boolean returnValue = false;
            using (SqlConnection myConnection = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
            {
                SqlCommand storedProcCommand = new SqlCommand(StoredProcedureName, myConnection);
                storedProcCommand.CommandType = CommandType.StoredProcedure;
                storedProcCommand.Parameters.AddWithValue(Parameter1Name, Value1);
                storedProcCommand.Parameters.AddWithValue(Parameter2Name, Value2);
                storedProcCommand.Parameters.AddWithValue(Parameter3Name, Value3);
                
                SqlDataReader myReader;
                try
                {
                    myConnection.Open(); 
                    myReader = storedProcCommand.ExecuteReader();
                    while (myReader.Read())
                        returnValue = (myReader.GetBoolean(0));
                    // always call Close when done reading.
                    myReader.Close();

                }
                //catch
                //{
                //    //Page.ClientScript.RegisterStartupScript(this.GetType(), "Error", "<script type=\"text/javascript\">alert('" + SQLDBException.Message + "');</script>");
                //    return false;
                //}
                catch { }

                finally
                {
                    // Close the connection when done with it.
                    myConnection.Close();

                }
            }
            return returnValue;


        }
        /////////////////////////////////////////////////////////////////////////////////////////

        public string ReadNivdalIntData(string ParameterName, string StoredProcedureName, Object Value)
        {
            //Int16 returnValue = 0;
            string returnValue = "";
            using (SqlConnection myConnection = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
            {
                SqlCommand storedProcCommand = new SqlCommand(StoredProcedureName, myConnection);
                storedProcCommand.CommandType = CommandType.StoredProcedure;
                storedProcCommand.Parameters.AddWithValue(ParameterName, Value);
                
                SqlDataReader myReader;
                try
                {
                    myConnection.Open(); 
                    myReader = storedProcCommand.ExecuteReader();
                    while (myReader.Read())
                        returnValue = (myReader.GetSqlDecimal(0).ToString());
                    // always call Close when done reading.
                    myReader.Close();

                }
                //catch (Exception SQLDBException)
                //{

                //    //Page.ClientScript.RegisterStartupScript(this.GetType(), "Error", "<script type=\"text/javascript\">alert('" + SQLDBException.Message + "');</script>");
                //    return Convert.ToString(SQLDBException);
                //    //return 4;
                //}
                catch { }

                finally
                {
                    // Close the connection when done with it.
                    myConnection.Close();

                }
            }
            return returnValue;


        }

        /////////////////////////////////////////////////////////////////////////////////////////

        public string ReadNivdalIntData(string Parameter1Name, string Parameter2Name, string StoredProcedureName, Object Value1, Object Value2)
        {
            //Int16 returnValue = 0;
            string returnValue = "";
            using (SqlConnection myConnection = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
            {
                SqlCommand storedProcCommand = new SqlCommand(StoredProcedureName, myConnection);
                storedProcCommand.CommandType = CommandType.StoredProcedure;
                storedProcCommand.Parameters.AddWithValue(Parameter1Name, Value1);
                storedProcCommand.Parameters.AddWithValue(Parameter2Name, Value2);
                
                SqlDataReader myReader;
                try
                {
                    myConnection.Open(); 
                    myReader = storedProcCommand.ExecuteReader();
                    while (myReader.Read())
                        returnValue = (myReader.GetSqlDecimal(0).ToString());
                    // always call Close when done reading.
                    myReader.Close();

                }
                //catch (Exception SQLDBException)
                //{

                //    //Page.ClientScript.RegisterStartupScript(this.GetType(), "Error", "<script type=\"text/javascript\">alert('" + SQLDBException.Message + "');</script>");
                //    return Convert.ToString(SQLDBException);
                //    //return 4;
                //}
                catch { }

                finally
                {
                    // Close the connection when done with it.
                    myConnection.Close();

                }
            }
            return returnValue;


        }

        /////////////////////////////////////////////////////////////////////////////////////////

        public string ReadNivdalIntData(string StoredProcedureName)
        {
            //Int16 returnValue = 0;
            string returnValue = "";
            using (SqlConnection myConnection = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
            {
                SqlCommand storedProcCommand = new SqlCommand(StoredProcedureName, myConnection);
                storedProcCommand.CommandType = CommandType.StoredProcedure;
                
                SqlDataReader myReader;
                try
                {
                    myConnection.Open(); 
                    myReader = storedProcCommand.ExecuteReader();
                    while (myReader.Read())
                        returnValue = (myReader.GetSqlDecimal(0).ToString());
                    // always call Close when done reading.
                    myReader.Close();

                }
                //catch (Exception SQLDBException)
                //{

                //    //Page.ClientScript.RegisterStartupScript(this.GetType(), "Error", "<script type=\"text/javascript\">alert('" + SQLDBException.Message + "');</script>");
                //    return Convert.ToString(SQLDBException);
                //}
                catch { }

                finally
                {
                    // Close the connection when done with it.
                    myConnection.Close();

                }
            }
            return returnValue;


        }
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

        public string ReadNivdalStringData(string ParameterName, string StoredProcedureName, Object Value)
        {
            //Int16 returnValue = 0;
            string returnValue = "";
            using (SqlConnection myConnection = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
            {
                SqlCommand storedProcCommand = new SqlCommand(StoredProcedureName, myConnection);
                storedProcCommand.CommandType = CommandType.StoredProcedure;
                storedProcCommand.Parameters.AddWithValue(ParameterName, Value);
                
                SqlDataReader myReader;
                try
                {
                    myConnection.Open(); 
                    myReader = storedProcCommand.ExecuteReader();
                    while (myReader.Read())
                        returnValue = (myReader.GetSqlString(0).ToString());
                    // always call Close when done reading.
                    myReader.Close();

                }
                //catch (Exception SQLDBException)
                //{

                //    //Page.ClientScript.RegisterStartupScript(this.GetType(), "Error", "<script type=\"text/javascript\">alert('" + SQLDBException.Message + "');</script>");
                //    return Convert.ToString(SQLDBException);
                //    //return 4;
                //}
                catch { }

                finally
                {
                    // Close the connection when done with it.
                    myConnection.Close();

                }
            }
            return returnValue;


        }


        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

        public string ReadNivdalStringData(string ParameterName1, string ParameterName2, string ParameterName3, string StoredProcedureName, Object Value1, Object Value2, Object Value3)
        {
            //Int16 returnValue = 0;
            string returnValue = "error";
            using (SqlConnection myConnection = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
            {
                SqlCommand storedProcCommand = new SqlCommand(StoredProcedureName, myConnection);
                storedProcCommand.CommandType = CommandType.StoredProcedure;
                storedProcCommand.Parameters.AddWithValue(ParameterName1, Value1);
                storedProcCommand.Parameters.AddWithValue(ParameterName2, Value2);
                storedProcCommand.Parameters.AddWithValue(ParameterName3, Value3);
                
                SqlDataReader myReader;
                try
                {
                    myConnection.Open(); 
                    myReader = storedProcCommand.ExecuteReader();
                    while (myReader.Read())
                        returnValue = (myReader.GetSqlString(0).ToString());
                    // always call Close when done reading.
                    myReader.Close();

                }
                catch (Exception SQLDBException)
                {

                    //Page.ClientScript.RegisterStartupScript(this.GetType(), "Error", "<script type=\"text/javascript\">alert('" + SQLDBException.Message + "');</script>");
                    return Convert.ToString(SQLDBException);
                    //return 4;
                }

                finally
                {
                    // Close the connection when done with it.
                    myConnection.Close();

                }
            }
            return returnValue;


        }
        
        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        public string ExecNivdalSQL(string ParameterName1, string ParameterName2, string ParameterName3, string ParameterName4, string ParameterName5, string ParameterName6, string StoredProcedureName, Object Value1, Object Value2, Object Value3, Object Value4, Object Value5, Object Value6)
        {
            //Int16 returnValue = 0;
            string returnValue = "";
            using (SqlConnection myConnection = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
            {
                SqlCommand storedProcCommand = new SqlCommand(StoredProcedureName, myConnection);
                storedProcCommand.CommandType = CommandType.StoredProcedure;
                storedProcCommand.Parameters.AddWithValue(ParameterName1, Value1);
                storedProcCommand.Parameters.AddWithValue(ParameterName2, Value2);
                storedProcCommand.Parameters.AddWithValue(ParameterName3, Value3);
                storedProcCommand.Parameters.AddWithValue(ParameterName4, Value4);
                storedProcCommand.Parameters.AddWithValue(ParameterName5, Value5);
                storedProcCommand.Parameters.AddWithValue(ParameterName6, Value6);
                
                SqlDataReader myReader;
                try
                {
                    myConnection.Open(); 
                    myReader = storedProcCommand.ExecuteReader();
                    while (myReader.Read())
                        returnValue = (myReader.GetSqlString(0).ToString());
                    // always call Close when done reading.
                    myReader.Close();

                }
                //catch (Exception SQLDBException)
                //{

                //    //Page.ClientScript.RegisterStartupScript(this.GetType(), "Error", "<script type=\"text/javascript\">alert('" + SQLDBException.Message + "');</script>");
                //    return Convert.ToString(SQLDBException);
                //    //return 4;
                //}
                catch { }

                finally
                {
                    // Close the connection when done with it.
                    myConnection.Close();

                }

            }
            return returnValue;


        }
        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        public string ExecNivdalSQL(string ParameterName1, string ParameterName2, string ParameterName3, string ParameterName4, string ParameterName5, string StoredProcedureName, Object Value1, Object Value2, Object Value3, Object Value4, Object Value5)
        {
            //Int16 returnValue = 0;
            string returnValue = "";
            using (SqlConnection myConnection = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
            {
                SqlCommand storedProcCommand = new SqlCommand(StoredProcedureName, myConnection);
                storedProcCommand.CommandType = CommandType.StoredProcedure;
                storedProcCommand.Parameters.AddWithValue(ParameterName1, Value1);
                storedProcCommand.Parameters.AddWithValue(ParameterName2, Value2);
                storedProcCommand.Parameters.AddWithValue(ParameterName3, Value3);
                storedProcCommand.Parameters.AddWithValue(ParameterName4, Value4);
                storedProcCommand.Parameters.AddWithValue(ParameterName5, Value5);
                
                SqlDataReader myReader;
                try
                {
                    myConnection.Open(); 
                    myReader = storedProcCommand.ExecuteReader();
                    while (myReader.Read())
                        returnValue = (myReader.GetSqlString(0).ToString());
                    // always call Close when done reading.
                    myReader.Close();

                }
                //catch (Exception SQLDBException)
                //{

                //    //Page.ClientScript.RegisterStartupScript(this.GetType(), "Error", "<script type=\"text/javascript\">alert('" + SQLDBException.Message + "');</script>");
                //    return Convert.ToString(SQLDBException);
                //    //return 4;
                //}
                catch { }

                finally
                {
                    // Close the connection when done with it.
                    myConnection.Close();

                }

            }
            return returnValue;


        }

        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

        public string ReadNivdalStringData(string ParameterName1, string ParameterName2, string ParameterName3,
                                           string ParameterName4, string ParameterName5, string ParameterName6,
                                           string ParameterName7, string ParameterName8, string ParameterName9,
                                            string StoredProcedureName,
                                            Object Value1, Object Value2, Object Value3, Object Value4, Object Value5,
                                             Object Value6, Object Value7, Object Value8, Object Value9)
        {
            //Int16 returnValue = 0;
            string returnValue = "";
            using (SqlConnection myConnection = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
            {
                SqlCommand storedProcCommand = new SqlCommand(StoredProcedureName, myConnection);
                storedProcCommand.CommandType = CommandType.StoredProcedure;
                storedProcCommand.Parameters.AddWithValue(ParameterName1, Value1);
                storedProcCommand.Parameters.AddWithValue(ParameterName2, Value2);
                storedProcCommand.Parameters.AddWithValue(ParameterName3, Value3);
                storedProcCommand.Parameters.AddWithValue(ParameterName4, Value4);
                storedProcCommand.Parameters.AddWithValue(ParameterName5, Value5);
                storedProcCommand.Parameters.AddWithValue(ParameterName6, Value6);
                storedProcCommand.Parameters.AddWithValue(ParameterName7, Value7);
                storedProcCommand.Parameters.AddWithValue(ParameterName8, Value8);
                storedProcCommand.Parameters.AddWithValue(ParameterName9, Value9);
                
                SqlDataReader myReader;
                try
                {
                    myConnection.Open(); 
                    myReader = storedProcCommand.ExecuteReader();
                    while (myReader.Read())
                        returnValue = (myReader.GetSqlString(0).ToString());
                    // always call Close when done reading.
                    myReader.Close();

                }
                //catch (Exception SQLDBException)
                //{

                //    //Page.ClientScript.RegisterStartupScript(this.GetType(), "Error", "<script type=\"text/javascript\">alert('" + SQLDBException.Message + "');</script>");
                //    return Convert.ToString(SQLDBException);
                //    //return 4;
                //}
                catch { }

                finally
                {
                    // Close the connection when done with it.
                    myConnection.Close();

                }
            }
            return returnValue;


        }

        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

        public string ReadNivdalStringData(string ParameterName1, string ParameterName2, string ParameterName3, string ParameterName4, string StoredProcedureName, Object Value1, Object Value2, Object Value3, Object Value4)
        {
            //Int16 returnValue = 0;
            string returnValue = "";
            using (SqlConnection myConnection = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
            {
                SqlCommand storedProcCommand = new SqlCommand(StoredProcedureName, myConnection);
                storedProcCommand.CommandType = CommandType.StoredProcedure;
                storedProcCommand.Parameters.AddWithValue(ParameterName1, Value1);
                storedProcCommand.Parameters.AddWithValue(ParameterName2, Value2);
                storedProcCommand.Parameters.AddWithValue(ParameterName3, Value3);
                storedProcCommand.Parameters.AddWithValue(ParameterName4, Value4);
                SqlDataReader myReader;
                try
                {
                    myConnection.Open();
                    myReader = storedProcCommand.ExecuteReader();
                    while (myReader.Read())
                        returnValue = (myReader.GetSqlString(0).ToString());
                    // always call Close when done reading.
                    myReader.Close();

                }
                //catch (Exception SQLDBException)
                //{

                //    //Page.ClientScript.RegisterStartupScript(this.GetType(), "Error", "<script type=\"text/javascript\">alert('" + SQLDBException.Message + "');</script>");
                //    return Convert.ToString(SQLDBException);
                //    //return 4;
                //}
                catch { }

                finally
                {
                    // Close the connection when done with it.
                    myConnection.Close();

                }
            }
            return returnValue;


        }

        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        public string ReadNivdalStringData(string ParameterName1, string ParameterName2, string ParameterName3, string ParameterName4, string ParameterName5,
            string StoredProcedureName, Object Value1, Object Value2, Object Value3, Object Value4, Object Value5)
        {
            //Int16 returnValue = 0;
            string returnValue = "";
            using (SqlConnection myConnection = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
            {
                SqlCommand storedProcCommand = new SqlCommand(StoredProcedureName, myConnection);
                storedProcCommand.CommandType = CommandType.StoredProcedure;
                storedProcCommand.Parameters.AddWithValue(ParameterName1, Value1);
                storedProcCommand.Parameters.AddWithValue(ParameterName2, Value2);
                storedProcCommand.Parameters.AddWithValue(ParameterName3, Value3);
                storedProcCommand.Parameters.AddWithValue(ParameterName4, Value4);
                storedProcCommand.Parameters.AddWithValue(ParameterName5, Value5);
                SqlDataReader myReader;
                try
                {
                    myConnection.Open();
                    myReader = storedProcCommand.ExecuteReader();
                    while (myReader.Read())
                        returnValue = (myReader.GetSqlString(0).ToString());
                    // always call Close when done reading.
                    myReader.Close();

                }
                //catch (Exception SQLDBException)
                //{

                //    //Page.ClientScript.RegisterStartupScript(this.GetType(), "Error", "<script type=\"text/javascript\">alert('" + SQLDBException.Message + "');</script>");
                //    return Convert.ToString(SQLDBException);
                //    //return 4;
                //}
                catch { }
                finally
                {
                    // Close the connection when done with it.
                    myConnection.Close();

                }
            }
            return returnValue;


        }

        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        public string ReadNivdalStringData(string ParameterName1, string ParameterName2, string ParameterName3, string ParameterName4, string ParameterName5,
            string ParameterName6, string StoredProcedureName, Object Value1, Object Value2, Object Value3, Object Value4, Object Value5, Object Value6)
        {
            //Int16 returnValue = 0;
            string returnValue = "";
            using (SqlConnection myConnection = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
            {
                SqlCommand storedProcCommand = new SqlCommand(StoredProcedureName, myConnection);
                storedProcCommand.CommandType = CommandType.StoredProcedure;
                storedProcCommand.Parameters.AddWithValue(ParameterName1, Value1);
                storedProcCommand.Parameters.AddWithValue(ParameterName2, Value2);
                storedProcCommand.Parameters.AddWithValue(ParameterName3, Value3);
                storedProcCommand.Parameters.AddWithValue(ParameterName4, Value4);
                storedProcCommand.Parameters.AddWithValue(ParameterName5, Value5);
                storedProcCommand.Parameters.AddWithValue(ParameterName6, Value6);
                SqlDataReader myReader;
                try
                {
                    myConnection.Open();
                    myReader = storedProcCommand.ExecuteReader();
                    while (myReader.Read())
                        returnValue = (myReader.GetSqlString(0).ToString());
                    // always call Close when done reading.
                    myReader.Close();

                }
                //catch (Exception SQLDBException)
                //{

                //    //Page.ClientScript.RegisterStartupScript(this.GetType(), "Error", "<script type=\"text/javascript\">alert('" + SQLDBException.Message + "');</script>");
                //    return Convert.ToString(SQLDBException);
                //    //return 4;
                //}
                catch { }
                finally
                {
                    // Close the connection when done with it.
                    myConnection.Close();

                }
            }
            return returnValue;


        }

        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


        public string ReadNivdalStringData(string ParameterName1, string ParameterName2,  string StoredProcedureName, Object Value1, Object Value2)
        {
            //Int16 returnValue = 0;
            string returnValue = "";
            using (SqlConnection myConnection = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
            {
                SqlCommand storedProcCommand = new SqlCommand(StoredProcedureName, myConnection);
                storedProcCommand.CommandType = CommandType.StoredProcedure;
                storedProcCommand.Parameters.AddWithValue(ParameterName1, Value1);
                storedProcCommand.Parameters.AddWithValue(ParameterName2, Value2);
                SqlDataReader myReader;
                try
                {
                    myConnection.Open();
                    myReader = storedProcCommand.ExecuteReader();
                    while (myReader.Read())
                        returnValue = (myReader.GetSqlString(0).ToString());
                    // always call Close when done reading.
                    myReader.Close();

                }
                //catch (Exception SQLDBException)
                //{

                //    //Page.ClientScript.RegisterStartupScript(this.GetType(), "Error", "<script type=\"text/javascript\">alert('" + SQLDBException.Message + "');</script>");
                //    return Convert.ToString(SQLDBException);
                //    //return 4;
                //}
                catch { }

                finally
                {
                    // Close the connection when done with it.
                    myConnection.Close();

                }
            }
            return returnValue;


        }
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

        public string ReadNivdalStringData(string StoredProcedureName)
        {
            //Int16 returnValue = 0;
            string returnValue = "";
            using (SqlConnection myConnection = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
            {
                SqlCommand storedProcCommand = new SqlCommand(StoredProcedureName, myConnection);
                storedProcCommand.CommandType = CommandType.StoredProcedure;
                SqlDataReader myReader;
                try
                {
                    myConnection.Open();
                    myReader = storedProcCommand.ExecuteReader();
                    while (myReader.Read())
                        returnValue = (myReader.GetSqlString(0).ToString());
                    // always call Close when done reading.
                    myReader.Close();

                }
                //catch (Exception SQLDBException)
                //{

                //    //Page.ClientScript.RegisterStartupScript(this.GetType(), "Error", "<script type=\"text/javascript\">alert('" + SQLDBException.Message + "');</script>");
                //    return Convert.ToString(SQLDBException);
                //    //return 4;
                //}
                catch { }

                finally
                {
                    // Close the connection when done with it.
                    myConnection.Close();

                }
            }
            return returnValue;


        }


        public string ExecNivdalSQL(string ParameterName1, string ParameterName2, string StoredProcedureName, Object Value1, Object Value2)
        {
            string returnValue = "";
            using (SqlConnection myConnection = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
            {
                SqlCommand storedProcCommand = new SqlCommand(StoredProcedureName, myConnection);
                SqlDataReader myReader;
                storedProcCommand.CommandType = CommandType.StoredProcedure;
                storedProcCommand.Parameters.AddWithValue(ParameterName1, Value1);
                storedProcCommand.Parameters.AddWithValue(ParameterName2, Value2);

                try
                {
                    myConnection.Open();
                    myReader = storedProcCommand.ExecuteReader();
                    while (myReader.Read())
                        returnValue = (myReader.GetSqlString(0).ToString());
                    myReader.Close();
                }
                //catch (Exception SQLDBException)
                //{
                //    return Convert.ToString(SQLDBException);
                //}
                catch { }
                finally
                {
                    // Close the connection when done with it.
                    myConnection.Close();

                }
            }
            return returnValue;


        }

        public string ExecNivdalSQL(string ParameterName1, string ParameterName2, string ParameterName3, string StoredProcedureName, Object Value1, Object Value2, Object Value3)
        {
            string returnValue = "";
            using (SqlConnection myConnection = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
            {
                SqlCommand storedProcCommand = new SqlCommand(StoredProcedureName, myConnection);
                SqlDataReader myReader;
                storedProcCommand.CommandType = CommandType.StoredProcedure;
                storedProcCommand.Parameters.AddWithValue(ParameterName1, Value1);
                storedProcCommand.Parameters.AddWithValue(ParameterName2, Value2);
                storedProcCommand.Parameters.AddWithValue(ParameterName3, Value3);

                try
                {
                    myConnection.Open();
                    myReader = storedProcCommand.ExecuteReader();
                    while (myReader.Read())
                        returnValue = (myReader.GetSqlString(0).ToString());
                    myReader.Close();
                }
                //catch (Exception SQLDBException)
                //{
                //    return Convert.ToString(SQLDBException);
                //}
                catch { }

                finally
                {
                    // Close the connection when done with it.
                    myConnection.Close();

                }
            }
            return returnValue;


        }

        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        public string ExecNivdalSQL(string ParameterName1, string StoredProcedureName, Object Value1)
        {
            string returnValue = "";
            using (SqlConnection myConnection = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
            {
                SqlCommand storedProcCommand = new SqlCommand(StoredProcedureName, myConnection);
                SqlDataReader myReader;
                storedProcCommand.CommandType = CommandType.StoredProcedure;
                storedProcCommand.Parameters.AddWithValue(ParameterName1, Value1);

                try
                {
                    myConnection.Open();
                    myReader = storedProcCommand.ExecuteReader();
                    returnValue = "ok";
                    while (myReader.Read())
                    {
                        returnValue = (myReader.GetSqlString(0).ToString());
                    }
                    myReader.Close();
                }
                //catch (Exception SQLDBException)
                //{
                //    return Convert.ToString(SQLDBException);
                //}
                catch { }

                finally
                {
                    // Close the connection when done with it.
                    myConnection.Close();

                }
            }
            return returnValue;


        }
        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        public string ExecNivdalSQL(string StoredProcedureName)
        {
            string returnValue = "";
            using (SqlConnection myConnection = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
            {
                SqlCommand storedProcCommand = new SqlCommand(StoredProcedureName, myConnection);
                SqlDataReader myReader;
                storedProcCommand.CommandType = CommandType.StoredProcedure;

                try
                {
                    myConnection.Open();
                    myReader = storedProcCommand.ExecuteReader();
                    while (myReader.Read())
                        returnValue = (myReader.GetSqlString(0).ToString());
                    myReader.Close();
                }
                //catch (Exception SQLDBException)
                //{
                //    return Convert.ToString(SQLDBException);
                //}
                catch { }
                finally
                {
                    // Close the connection when done with it.
                    myConnection.Close();

                }
            }
            return returnValue;


        }

        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

        public string ExecNivdalSQL(string ParameterName1, string ParameterName2, string ParameterName3, string ParameterName4, string StoredProcedureName, Object Value1, Object Value2, Object Value3, Object Value4)
        {
            //Int16 returnValue = 0;
            string returnValue = "";
            using (SqlConnection myConnection = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
            {
                SqlCommand storedProcCommand = new SqlCommand(StoredProcedureName, myConnection);
                storedProcCommand.CommandType = CommandType.StoredProcedure;
                storedProcCommand.Parameters.AddWithValue(ParameterName1, Value1);
                storedProcCommand.Parameters.AddWithValue(ParameterName2, Value2);
                storedProcCommand.Parameters.AddWithValue(ParameterName3, Value3);
                storedProcCommand.Parameters.AddWithValue(ParameterName4, Value4);
                SqlDataReader myReader;
                try
                {
                    myConnection.Open();
                    myReader = storedProcCommand.ExecuteReader();
                    while (myReader.Read())
                        returnValue = (myReader.GetSqlString(0).ToString());
                    // always call Close when done reading.
                    myReader.Close();

                }
                //catch (Exception SQLDBException)
                //{
                //    return Convert.ToString(SQLDBException);
                //}
                catch { }

                finally
                {
                    // Close the connection when done with it.
                    myConnection.Close();

                }
            
            }
            return returnValue;


        }
       
        public string SendPendingMails()
        {
            string NumUnsentMails = ReadNivdalIntData("Nivdal_GetNumMailsToSend");
            int NumSentMails = 0;
            if (NumUnsentMails != "0")
            {
                LinkedList<decimal> MailIds = new LinkedList<decimal>();
                // This means that there are mails to send.Sending them
                using (SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
                {
                    SqlConnection conn1 = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
                    SqlCommand cmdRead = new SqlCommand("Nivdal_GetMailsToSend", conn);
                    SqlCommand cmdDone = new SqlCommand("Nivdal_SetMailAsSent", conn);
                    cmdDone.CommandType = CommandType.StoredProcedure;
                    cmdRead.CommandType = CommandType.StoredProcedure;
                    SqlDataReader reader;
                    string Destination;
                    string Subject;
                    string Body;
                    decimal MailId;
                    string SendFrom = "admin@nivdal.com";
                    //string SendFrom = "nivdal@gmail.com";
                    MailMessage MailMessage;
                    SmtpClient emailClient;
                    bool ToUpdate = true;
                    try
                    {
                        conn.Open();
                        reader = cmdRead.ExecuteReader();

                        if (reader.HasRows)
                        {
                            while (reader.Read())
                            {
                                // read mail details
                                MailId = reader.GetDecimal(0);
                                Destination = reader.GetString(1);
                                Subject = reader.GetString(2);
                                Body = reader.GetString(3);

                                // send the mail.
                                try
                                {
                                    MailMessage = new MailMessage(SendFrom, Destination, Subject, Body);
                                    
                                    // making it HTML ...
                                    MailMessage.Headers.Add("Content-Type", "content=text/html; charset=\"UTF-8\"");
                                    MailMessage.IsBodyHtml = true;

                                    MailMessage.BodyEncoding = System.Text.Encoding.UTF8;
                                    //emailClient = new SmtpClient("127.0.0.1");
                                    emailClient = new SmtpClient();
                                    // this is gmail only
                                    //emailClient.EnableSsl = true;
                                    emailClient.Send(MailMessage);
                                }
                                catch
                                {
                                    //Response.Write("mail send failed");
                                    ToUpdate = false;
                                }
                                finally { }

                                // update mail as sent
                                if (ToUpdate)
                                {
                                    MailIds.AddLast(MailId);
                                }
                                ToUpdate = true;

                            }
                        }
                        reader.Close();

                        // Updating that the mails were sent
                        foreach (decimal Id in MailIds)
                        {
                            cmdDone.Parameters.Clear();
                            cmdDone.Parameters.Add("@MailId", SqlDbType.Decimal, 12);
                            cmdDone.Parameters["@MailId"].Direction = ParameterDirection.Input;
                            cmdDone.Parameters["@MailId"].Value = Id;

                            try
                            {
                                conn1.Open();
                                cmdDone.ExecuteNonQuery();
                                NumSentMails++;
                            }
                            finally
                            {
                                conn1.Close();
                            }
                        }
                    }
                    catch { }
                    finally
                    {
                        conn.Close();
                    }
                }
            }

            return NumSentMails.ToString();
        }



        public string GetUserCulture()
        {
            string UserId = ReturnUserId();
            string Culture = "en-US";
            if(UserId != null) 
            {
                Culture = ReadNivdalStringData("@UserId", "Nivdal_GetUserCulture", UserId);
            }
            else
            {
                Culture = "en-US";
            }

            return (Culture);
            
        }

        public string GetUserUICulture()
        {
            string UserId = ReturnUserId();
            string UiCulture = "en";
            if(UserId != null)
            {
                UiCulture = ReadNivdalStringData("@UserId", "Nivdal_GetUserUICulture", UserId);
            }
            else
            {
                UiCulture = "en";
            }
            
            return (UiCulture);
        }

        public string GetUserLanguage()
        {
            return (ReadNivdalIntData("UserId", "Nivdal_GetUserLanguage", ReturnUserId()));
        }

        public string ReturnAppTitle()
        {

            return ("Nivdal.com!");
        }

        public string InjuryIconName(object injury)
        {
            string ReturnValue = "";
            int Inj;
            try
            {
                Inj = int.Parse(injury.ToString());
                if (Inj == 0)
                    ReturnValue = "";
                else if (Inj >= 21 && Inj <= 23)
                    ReturnValue = "~/Images/Icons/IP1.gif";
                else if (Inj >= 24 && Inj <= 26)
                    ReturnValue = "~/Images/Icons/IP2.gif";
                else if (Inj >= 27 && Inj <= 29)
                    ReturnValue = "~/Images/Icons/IP3.gif";
                else if (Inj == 30)
                    ReturnValue = "~/Images/Icons/IP4.gif";
                else if (Inj >= 31 && Inj <= 33)
                    ReturnValue = "~/Images/Icons/INP1.gif";
                else if (Inj >= 34 && Inj <= 36)
                    ReturnValue = "~/Images/Icons/INP2.gif";
                else if (Inj >= 37 && Inj <= 39)
                    ReturnValue = "~/Images/Icons/INP3.gif";
                else if (Inj == 40)
                    ReturnValue = "~/Images/Icons/INP4.gif";
                else if (Inj >= 40 && Inj <= 50)
                    ReturnValue = "~/Images/Icons/IP8.gif";
                else if (Inj >= 50 && Inj <= 60)
                    ReturnValue = "~/Images/Icons/INP8.gif";
                else
                    ReturnValue = "~/Images/Icons/players_healthy.jpg";
            }
            catch { }
            return ReturnValue;        

        }

    }








}

